/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.apt.connectDB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Tools {

    public static int getTotalPage(String tablename) {
        int total = 0;
        try {
            Connection con = MyConnect.getConnection();
            Statement st = con.createStatement();
            String sql = "SELECT * FROM " + tablename + "";
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                total++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return total;
    }

    public static int getTotalPage(String tablename, String columnName, String stringSerach) {
        int total = 0;
        try {
            Connection con = MyConnect.getConnection();
            Statement st = con.createStatement();
            String sql = "SELECT * FROM " + tablename + " where " + columnName + " LIKE N'%" + stringSerach + "%'";
            System.out.println(sql);
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                total++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return total;
    }

    public static Timestamp convertStringToTimestamp(String str) {
        Timestamp ts = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyyy");
            sdf.setLenient(false);
            Date d = sdf.parse(str);
            ts = new Timestamp(d.getTime());
        } catch (Exception ex) {
            Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, "convertStringToTimestamp", ex);
            return null;
        }
        return ts;
    }

    public static String convertTimestampToString(Timestamp ts) {
        String date = "";
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(ts.getTime());
            int d = cal.get(Calendar.DAY_OF_MONTH);
            int m = cal.get(Calendar.MONTH) + 1;
            int y = cal.get(Calendar.YEAR);

            String day = (d < 10) ? "0" + d : "" + d;
            String month = (m < 10) ? "0" + m : "" + m;

            date = month + "/" + day + "/" + y;

        } catch (Exception ex) {
            Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, "convertTimestampToString", ex);
            return null;
        }
        return date;
    }

    public static void main(String[] args) {
        
    }
}
